package com.kdzy.lshy.common.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;


/**
 * @Auther: hanrui
 * @Date: 2018/6/22
 * @Description:
 */

@Controller
@CrossOrigin("*")
@RequestMapping("/")

public class Test {



    @RequestMapping("/download")
    public void get(HttpServletResponse response) throws IOException {

        response.setContentType("application/octet-stream");

        response.setHeader("Content-disposition", "attachment;filename=" + new String("办事指南".getBytes("UTF-8"),"ISO8859-1")+".jpg");

        //查询
        //String path=""

        String path = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1530276222&di=346d00a523727de2f2fe293d40c51f89&imgtype=jpg&er=1&src=http%3A%2F%2Fwww.myexception.cn%2Fimg%2F2014%2F08%2F06%2F10503415.jpg";

        InputStream fis = getInputStreamByUrl(path);
        OutputStream out = response.getOutputStream();

        int length = 0;
        byte[] buf = new byte[1024];
        while ((length = fis.read(buf)) > 0) {
            out.write(buf, 0, length);
        }
        out.flush();
        out.close();
    }




    public static InputStream getInputStreamByUrl(String urlStr) {
        DataInputStream in = null;
        try {
            URL url = new URL(urlStr);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            in = new DataInputStream(conn.getInputStream());
        } catch (IOException e) {

        }
        return in;
    }


}
